package com.tangxz.chat.common.dto;

import lombok.Data;
import lombok.ToString;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;

import java.util.ArrayList;
import java.util.List;

/**
 * mongodb存储的数据结构
 * @author: 唐小尊
 * @email: 1171702529@qq.com
 * @cate: 2021/04/19 20:04
 */

@Data
@ToString
public class MsgGroupDto {
    //发送者id
    @Id
    //新增唯一键 百万级查询效率可以从两分钟优化到几秒钟
    @Indexed(unique = true)
    private String senderId;
    private Integer unreadNum;
    private List<Msg> msgList;

    public MsgGroupDto(String senderId, Integer unreadNum, List<Msg> msgList) {
        this.senderId = senderId;
        this.unreadNum = unreadNum;
        this.msgList = msgList;
    }

    public void add(Msg msg){
        if (this.msgList==null){
            this.msgList = new ArrayList<>();
        }
        msgList.add(msg);
        unreadNum++;
    }
}
